package priv.lirenhe.dissertation.component.crawler_robot.url_manager.url_manager_executor;

import java.util.HashMap;
import java.util.Map;

import priv.lirenhe.dissertation.bdb.url_queue.NotUsedUrlQueue;
import priv.lirenhe.dissertation.bdb.url_queue.UsedUrlQueue;
import priv.lirenhe.dissertation.bdb.util.UrlQueUtils;
import priv.lirenhe.dissertation.component.crawler_robot.url_manager.url_deduplication.UrlQueueDeduplication;
import priv.lirenhe.dissertation.component.crawler_robot.url_manager.url_ini.SeedUrlGetter;

public class UrlManagerExecutor {
	
	private static NotUsedUrlQueue notUsedUrlQueue;	
	private static UsedUrlQueue usedUrlQueue;
	
	public UrlManagerExecutor (){
		notUsedUrlQueue = UrlQueUtils.getNotUsedUrlQueue();
		usedUrlQueue = UrlQueUtils.getUsedUrlQueue();
	}
	
	/**
	 * 
	 * @return
	 */
	public Map<String,String> execute(){
		
		System.out.println("UrlManagerExecutor execute---");
		
		Map<String,String> exeMap = null;
		String realUrl = getRealUrl();
		int size = 0 ;
		size = getSizeOfUrlInDBD();
		
		if(realUrl != null){
			exeMap = new HashMap<String,String>();
			this.putUrlToUsedBDB(realUrl);
			exeMap.put("realUrl", realUrl);
			if(size >0){
				exeMap.put("size", String.valueOf(size));
			}
		}
		
		return exeMap;
	}
	
	/**
	 * 
	 * @return
	 */
	private String getRealUrl(){
		String url = null;
		boolean emptyFlag = true;
		synchronized (notUsedUrlQueue) {
			notUsedUrlQueue.syncBDB();
			emptyFlag = notUsedUrlQueue.isEmpty();
		}
		System.out.println("BDB中realUrl的个数="+notUsedUrlQueue.getSize());
		if(emptyFlag){
			
			System.out.println("==========notUsedUrlQueue为空===========");
			
			url = this.getSeedUrl();
			
			System.out.println("种子url="+url);
			
		}else{
			
			System.out.println("==========notUsedUrlQueue不为空===========");
			
			url = this.getNotUsedUrl();
			
			System.out.println("未使用队列中取出的url="+url);
			
		}
		if(url != null){
			while(this.isDuplication(url)){
				url = this.getRealUrl();
			}
		}
	
		return url;
	}
	
	/**
	 * 
	 * @return
	 */
	private String getSeedUrl(){
		String seedUrl = null;
		SeedUrlGetter.iniPropConfig();
		seedUrl = SeedUrlGetter.getOneSeedUrl();
		if(seedUrl != null){
			if(!seedUrl.startsWith("http:") && !seedUrl.startsWith("https:")){
				seedUrl = null;
			}
		}
		return seedUrl;
	}
	
	/**
	 * 
	 * @return
	 */
	private String getNotUsedUrl(){
		
		String notUsedUrlFromQueue = null;
		synchronized (notUsedUrlQueue) {
			try {
				notUsedUrlQueue.syncBDB();
				notUsedUrlFromQueue = notUsedUrlQueue.getNotUsedUrlAndDelete();
				if(notUsedUrlFromQueue != null){
					if(!notUsedUrlFromQueue.startsWith("http:") && !notUsedUrlFromQueue.startsWith("https:")){
						notUsedUrlFromQueue = null;
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return notUsedUrlFromQueue;
	}
	
	/**
	 * 
	 * @param url
	 * @return
	 */
	private boolean isDuplication(String url){
		UrlQueueDeduplication urlQueueDeduplication = new UrlQueueDeduplication();
		boolean flag = false;
		if(urlQueueDeduplication.usedUrlQueHasUsedThisUrl(url)){
			flag = true;
		}
		return flag;
	}
	
	/**
	 * 
	 * @param url
	 * @return
	 */
	private boolean putUrlToUsedBDB(String url){
		
		boolean flag = false;
		flag = usedUrlQueue.setusedUrl(url);
		return flag ;
	}
	
	public int getSizeOfUrlInDBD(){
		return notUsedUrlQueue.getNotUsedUrlSize();
	}
	
}
